require(['jquery','underscore','Mustache','Utils'],function($,_,Mustache,Utils){
	var templateHtml='<li href="#" class="list-group-item result-item mar-5" data-id={{_id}}>' 
      +'<div class="media-left">'
          +'<img class="media-object" data-src="holder.js/64x64" alt="" src="/image/icon/{{type}}-icon.png" data-holder-rendered="true" style="width: 64px; height: 64px;">'
      +'</div>'
      +'<div class="media-body">'
        +'<h4 class="media-heading"><span>{{source}}</span><span class="mar-left-10">{{name}}</span></h4>' 
        +'<ul class="pad-none">'
        +'<li class="mar-right-20 fl-l"><span>Qualified Name:</span><span class="mar-left-10">{{qualifiedName}}</span></li>'
        +'<li class="mar-right-20 fl-l"><span>Type:</span><span class="mar-left-10">{{type}}</span></li>'
        +'<li class="mar-right-20 fl-l"><span>Source:</span><span class="mar-left-10">{{source}}</span></li> '
        +'<li class="mar-right-20 fl-l"><span>Create Time:</span><span class="mar-left-10">{{createTime}}</span></li>'
        +'<li class="mar-right-20 fl-l"><span>Update Time:</span><span class="mar-left-10">{{updateTime}}</span></li>'
        +' <li class="mar-right-20 fl-l"><span>Owner:</span><span class="mar-left-10">{{owner}}</span></li>'
        +'</ul>'
      +'</div>'
      +'</li>';
	function search(keyword){
		$.getJSON('/entity/search/'+keyword,function(res){
			var array=res.data;
			var filter=res.filter;
			var filterContainers=$('.filter-container');
			$.each(filterContainers,function(i,e){
				var filterItems=$(e).find('li');
				var filterName=$(e).data('filter-name');
				var filterRes=filter[filterName];
				$.each(filterItems,function(i,e){
					var itemName=$(e).data('filter-item-name');
					$(e).find('.badge').text(filterRes[itemName]?filterRes[itemName]:'0');
				});
			});
			$('#result-num').text(array.length);
			$('#result-container').html('');
			for(var i in array){
				var data=array[i];
				try{
					data.createTime_str=Utils.time_format(data.createTime,'yyyy-MM-dd hh:mm:ss');
					data.updateTime_str=Utils.time_format(data.updateTime,'yyyy-MM-dd hh:mm:ss');
				}catch(e){}
				$('#result-container').append(
						'<li href="#" class="list-group-item result-item mar-5" data-id="'+data._id+'">' 
						      +'<div class="media-left">'
						          +'<img class="media-object" data-src="holder.js/64x64" alt="" src="/image/icon/'+data.type+'-icon.png" data-holder-rendered="true" style="width: 64px; height: 64px;">'
						      +'</div>'
						      +'<div class="media-body">'
						        +'<h4 class="media-heading"><span>'+data.source.replace(keyword,'<span class="highlight">'+keyword+'</span>')+'</span><span class="mar-left-10">'+data.name.replace(keyword,'<span class="highlight">'+keyword+'</span>')+'</span></h4>' 
						        +'<ul class="pad-none">'
						        +'<li class="mar-right-20 fl-l"><span>Qualified Name:</span><span class="mar-left-10">'+data.qualifiedName.replace(keyword,'<span class="highlight">'+keyword+'</span>')+'</span></li>'
						        +'<li class="mar-right-20 fl-l"><span>Type:</span><span class="mar-left-10">'+data.type.replace(keyword,'<span class="highlight">'+keyword+'</span>')+'</span></li>'
						        +'<li class="mar-right-20 fl-l"><span>Source:</span><span class="mar-left-10">'+data.source.replace(keyword,'<span class="highlight">'+keyword+'</span>')+'</span></li> '
						        +'<li class="mar-right-20 fl-l"><span>Create Time:</span><span class="mar-left-10">'+data.createTime+'</span></li>'
						        +'<li class="mar-right-20 fl-l"><span>Update Time:</span><span class="mar-left-10">'+data.updateTime+'</span></li>'
						        +' <li class="mar-right-20 fl-l"><span>Owner:</span><span class="mar-left-10">'+data.owner.replace(keyword,'<span class="highlight">'+keyword+'</span>')+'</span></li>'
						        +'</ul>'
						      +'</div>'
						      +'</li>'
				);
			}
		});
	}
	$('#btn-search').bind('click',function(){
		var keyword=$('#search-keyword').val();
		search(keyword);
	});
	$('#search-keyword').bind('keydown',function(e){
		if(e.keyCode==13){
			search($('#search-keyword').val());
		}
	});
	$('#result-container').delegate('li','click',function(){
		location.href='/detail.html?eid='+$(this).data('id');
	});
	$(document).ready(function(){
		var searchObj=Utils.search_parse();
		$('#search-keyword').val(searchObj['q']);
		search(searchObj['q']);
	});
});